home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Precision Software Appli…tions Silver Collection 4
/
Precision Software Applications Silver Collection Volume 4 (1993).iso
/
stats
/
sixsig.exe
/
SSS.C
next >
Wrap
Text File
|
1992-07-11
|
33KB
|
997 lines
/*
* Six Sigma Simulation (SSS) - This program graphically demonstrates
* histograms of pseudo-random i.i.d. Normal variables with mean zero and
* variance one.
*
* Source code for 5 functions are NOT included here because they are
* copyrighted materials that you may obtain directly from the publisher:
* ran1(), ran2(), ran3() and gasdev() are "portable" psuedo-random number
* generator functions of Press, et.al. (1988) "Numerical Recipes in C,"
* Ch.7, Cambridge University Press. Similarly, erfcc() is the compact
* error function of Chapter 6 of that same book.
*/
#include <graph.h> // _outtext, _settextcolor, _settextposition
#include <string.h> // strlen
#include <signal.h> // signal handler
#include <stdio.h> // sprintf
#include <stdlib.h> // toupper
#include <malloc.h> // malloc, free
#include <time.h> // time, clock
#include <conio.h> // kbhit
#include <math.h>
/* Macros */
#define square( x ) ( (x) * (x) )
#define _outxcr( ach, col, row ) { _settextposition( row, col ); \
_outtext( ach ); }
/* Constants */
#define ESC 27
#define BLANK 32
#define BLOCK 223 /* top-half bar; full=219 & bottom-half=220 */
#define EFRAC 0.75
#define TOP 1
#define LEFTCOLUMN 49
#define PROMPTPOS 25
#define WIDTH 31
#define HEIGHT (cszMenu + 2)
#define SINGLCOLOR 7
#define BACKCOLOR 0L
#define MENUCOLOR 3
#define MENUERASE 0
enum BOOL { FALSE, TRUE };
/* Structure type for colored bars */
typedef struct _HIST {
int length;
short color;
} HIST;
/* Global variables */
char *aszMenu[] = {
"",
" Six Sigma Simulator ",
"",
"True",
"Cumulative",
"Incremental",
"Lines",
"MenuOff NarrowCells",
"Parameters ResetAccum ",
"SampleSize WideCells ",
"",
"",
"",
"",
"",
"Press a Menu Letter Key",
" ( T C I L M N P R S W ) ",
"or Press ESCape to quit: "
};
char buf[256], yesno;
char savnam[13], parnam[13], Hbar[77], MenuCh[2], String[31];
int cszMenu = sizeof( aszMenu ) / sizeof( aszMenu[0] );
int dispMenu, dispType, topRow, botRow;
int reppbat, iter, tRows, iColorMax, dofinc, doftot, bwidth;
int idum, iduminit, firstobs, lastobs, firstcum, lastcum, prevC;
int bmtype, rntype, cellwid;
time_t *ltime, timer;
long counts[50], totals[50], reps, maxreps;
double zvalue, content, rroot2, pgreater, pequal;
double chisqinc, chisqtot, exptmin, fwidth;
double maxfrac, expfrac[50], exptail[50], cutval[50];
double obsfrac[50], chisqobs[50], cumfrac[50], chisqcum[50];
FILE *savfile, *parfile;
HIST abar[50]; /* max histogram bars = rows on screen */
struct videoconfig vc;
struct tm *curtime;
/* Function prototypes */
void main( void );
void handler( int sig );
void ClrScreen( void );
void DrawCell( int iRow );
void DrawFrame( int iTop, int Left, int Width, int Height );
void DrawHist( int Code );
void DrawMenu( int Color );
void ObsrvHist( void );
void ResetHist( void );
void SSSparm( void );
void SampSize( void );
int RunMenu( void );
double box_mull( int *idum );
float whran( int *idum );
float ranX( int *idum );
int initseed( void );
time_t exptime( time_t *ltime );
extern void exit( int code );
extern double erfcc( double x );
extern double gasdev( int *idum );
extern float ran1( int *idum );
extern float ran2( int *idum );
extern float ran3( int *idum );
void handler( int sig ) { /* Interrupt Handler */
signal( SIGINT, SIG_IGN ); /* ignore further signals */
_setvideomode( _DEFAULTMODE );
exit( 1 );
}
/*
* Brute Force Box-Muller Transformation.
*/
double box_mull( idum )
int *idum; {
static int iset=0;
static double next;
double rad,ang;
if( iset == 0 ) {
rad = sqrt( -2.0 * log( (double)ranX( idum ) ) );
ang = 2.0 * 3.1415926536 * (double)ranX( idum );
next = rad * cos( ang );
iset = 1;
return( rad * sin( ang ) );
}
else {
iset=0;
return next;
}
}
void DrawMenu( int Color ) { /* Color = 0 ==> erase */
int i;
_settextcolor( Color );
_setbkcolor( BACKCOLOR );
DrawFrame( TOP, LEFTCOLUMN - 3, WIDTH + 3, HEIGHT );
for( i = 0; i < cszMenu; i++ )
_outxcr( aszMenu[i], LEFTCOLUMN, TOP + i + 1 );
}
/*
* DrawFrame - Makes a rectangular frame using the double-line box
* characters. The parameters iTop, iLeft, iWidth, and iHeight are
* the row and column arguments for the upper-left and lower-right
* corners of the frame.
*/
void DrawFrame( int iTop, int iLeft, int iWidth, int iHeight ) {
enum { ULEFT = 201, URIGHT = 187,
LLEFT = 200, LRIGHT = 188,
VERTICAL = 186, HORIZONTAL = 205
};
int iRow;
char achTmp[50];
memset( achTmp, HORIZONTAL, iWidth );
achTmp[0] = (char)ULEFT;
achTmp[iWidth - 1] = (char)URIGHT;
achTmp[iWidth] = '\0';
_outxcr( achTmp, iLeft, iTop );
memset( achTmp, BLANK, iWidth );
achTmp[0] = (char)VERTICAL;
achTmp[iWidth - 1] = (char)VERTICAL;
for( iRow = iTop + 1; iRow <= iHeight; iRow++ )
_outxcr( achTmp, iLeft, iRow );
memset( achTmp, HORIZONTAL, iWidth );
achTmp[0] = (char)LLEFT;
achTmp[iWidth - 1] = (char)LRIGHT;
_outxcr( achTmp, iLeft, iTop + iHeight );
}
int RunMenu() {
char ch;
if( dispMenu == 0 ) {
if( !kbhit() )
return( 0 );
else {
dispMenu = 1;
_clearscreen( _GCLEARSCREEN );
bwidth = 42;
Hbar[bwidth] = '\0';
fwidth = 42.5;
prevC = -1;
DrawMenu( MENUCOLOR );
sprintf( String, "<<<" );
if( dispType == 0 ) {
_outxcr( String, LEFTCOLUMN + 20, TOP + 4 );
}
else if( dispType == 2 ) {
_outxcr( String, LEFTCOLUMN + 20, TOP + 5 );
}
else {
_outxcr( String, LEFTCOLUMN + 20, TOP + 6 );
}
sprintf( String, "Replications =%10ld", reps );
_outxcr( String, LEFTCOLUMN, TOP + 12 );
sprintf( String, "Cum. Chi Sq. =%10.2lf (%2d)", chisqtot, doftot );
_outxcr( String, LEFTCOLUMN, TOP + 13 );
sprintf( String, "Inc. Chi Sq. =%10.2lf (%2d)", chisqinc, dofinc );
_outxcr( String, LEFTCOLUMN, TOP + 14 );
_displaycursor( _GCURSOROFF );
while( kbhit() )
getch();
return( 0 );
}
}
_settextcolor( MENUCOLOR );
_setbkcolor( BACKCOLOR );
sprintf( String, "Replications =%10ld", reps );
_outxcr( String, LEFTCOLUMN, TOP + 12 );
sprintf( String, "Cum. Chi Sq. =%10.2lf (%2d)", chisqtot, doftot );
_outxcr( String, LEFTCOLUMN, TOP + 13 );
sprintf( String, "Inc. Chi Sq. =%10.2lf (%2d)", chisqinc, dofinc );
_outxcr( String, LEFTCOLUMN, TOP + 14 );
if( !kbhit() )
return( 0 );
_settextposition( TOP + cszMenu, LEFTCOLUMN + PROMPTPOS );
ch = (char)getch();
sprintf( MenuCh, "%c", toupper( ch ) );
_outtext( MenuCh );
while( kbhit() )
getch();
sprintf( String, " " );
_outxcr( String, LEFTCOLUMN + 20, TOP + 4 );
_outxcr( String, LEFTCOLUMN + 20, TOP + 5 );
_outxcr( String, LEFTCOLUMN + 20, TOP + 6 );
sprintf( String, "<<<" );
/* Branch to the appropriate procedure depending on the key. */
switch( toupper( ch ) ) { /* choices = T C I L M P R S */
case 'T':
dispType = 0;
break;
case 'C':
dispType = 2;
break;
case 'I':
dispType = 1;
break;
case 'L':
if( tRows == 25 )
tRows = 43;
else
tRows = 25;
prevC = -1;
ClrScreen();
DrawMenu( MENUCOLOR );
break;
case 'P':
_clearscreen( _GCLEARSCREEN );
SSSparm();
prevC = -1;
ResetHist();
ClrScreen();
DrawMenu( MENUCOLOR );
break;
case 'R':
_clearscreen( _GCLEARSCREEN );
ResetHist();
prevC = -1;
ClrScreen();
DrawMenu( MENUCOLOR );
break;
case 'S':
_clearscreen( _GCLEARSCREEN );
SampSize();
prevC = -1;
ClrScreen();
DrawMenu( MENUCOLOR );
break;
case 'M':
dispMenu = 0;
prevC = -1;
Hbar[bwidth] = (char)BLOCK;
bwidth = 77;
fwidth = 77.5;
DrawMenu( MENUERASE );
break;
case 'N':
if( cellwid == 2 )
cellwid = 1;
if( cellwid == 4 )
cellwid = 2;
prevC = -1;
break;
case 'W':
if( cellwid == 2 )
cellwid = 4;
if( cellwid == 1 )
cellwid = 2;
prevC = -1;
break;
case ESC:
return( 1 );
default: // Unknown key ignored
sprintf( MenuCh, " " );
break;
}
_settextposition( TOP + cszMenu, LEFTCOLUMN + PROMPTPOS );
_outtext( MenuCh );
if( dispType < 1 ) {
_outxcr( String, LEFTCOLUMN + 20, TOP + 4 );
}
else if( dispType > 1 ) {
_outxcr( String, LEFTCOLUMN + 20, TOP + 5 );
}
else {
_outxcr( String, LEFTCOLUMN + 20, TOP + 6 );
}
_displaycursor( _GCURSOROFF );
return( 0 );
}
void DrawHist( int Code ) {
int iRow, iLength, Separate, iColor;
double Fract;
if( Code == 0 && prevC == 0 )
return;
prevC = Code;
for( iRow = topRow; iRow <= botRow; iRow++ ) {
Separate = 0;
if( cellwid == 4 ) {
if( ( topRow == 3 && iRow < 5 ) ||
( botRow == 45 && iRow > 44 ) )
Separate = 1;
else if( ( topRow == 12 && iRow == 12 ) ||
( botRow == 36 && iRow == 36 ) )
Separate = 1;
else if( iRow % 4 == 1 ) {
if( Code == 0 )
Fract = ( expfrac[iRow] + expfrac[iRow+1] +
expfrac[iRow+2] + expfrac[iRow+3] ) / 4;
else if( Code == 1 )
Fract = ( obsfrac[iRow] + obsfrac[iRow+1] +
obsfrac[iRow+2] + obsfrac[iRow+3] ) / 4;
else
Fract = ( cumfrac[iRow] + cumfrac[iRow+1] +
cumfrac[iRow+2] + cumfrac[iRow+3] ) / 4;
iLength = (int)( EFRAC * fwidth * Fract / maxfrac );
}
}
if( cellwid == 2 ) {
if( ( topRow == 12 && iRow == 12 ) ||
( botRow == 36 && iRow == 36 ) )
Separate = 1;
else if( iRow % 2 == 1 ) {
if( Code == 0 )
Fract = ( expfrac[iRow] + expfrac[iRow+1] ) / 2;
else if( Code == 1 )
Fract = ( obsfrac[iRow] + obsfrac[iRow+1] ) / 2;
else
Fract = ( cumfrac[iRow] + cumfrac[iRow+1] ) / 2;
iLength = (int)( EFRAC * fwidth * Fract / maxfrac );
}
}
if( cellwid == 1 || Separate == 1 ) {
if( Code == 0 )
iLength = (int)( EFRAC * fwidth * expfrac[iRow] / maxfrac );
else if( Code == 1 )
iLength = (int)( EFRAC * fwidth * obsfrac[iRow] / maxfrac );
else
iLength = (int)( EFRAC * fwidth * cumfrac[iRow] / maxfrac );
}
if( iLength > bwidth )
iLength = bwidth;
abar[iRow-topRow].length = iLength;
if( iColorMax == 1 )
abar[iRow-topRow].color = (short)SINGLCOLOR;
else {
iColor = (int)( (double)(iLength*14) / (double)bwidth + 1.0 );
abar[iRow-topRow].color = (short)iColor;
}
DrawCell( iRow );
_displaycursor( _GCURSOROFF );
}
}
/*
* DrawCell - Prints a bar at a specified row using
* the length and color given in the abar work array.
*/
void DrawCell( int iRow ) {
int rValue, relRow;
char Label[3];
relRow = iRow-topRow;
_settextposition( relRow+1, 3 ); /* starting from column 3 */
_settextcolor( 0 );
_outtext( Hbar ); /* erase 42 or 77 positions */
if( abar[relRow].length > 0 ) {
_settextposition( relRow+1, 3 ); /* restart from column 3 */
_settextcolor( abar[relRow].color );
Hbar[abar[relRow].length] = '\0';
_outtext( Hbar );
if( abar[relRow].length < bwidth )
Hbar[abar[relRow].length] = (char)BLOCK;
}
if( ( iRow ) % 4 == 0 ) {
if( dispMenu == 1 ) {
rValue = 6 - iRow / 4;
sprintf( Label, "%2d", rValue );
_settextcolor( SINGLCOLOR );
_outxcr( Label, 1, relRow+1 );
}
else {
memset( Label, BLANK, 2 );
Label[2] = '\0';
_outxcr( Label, 1, relRow+1 );
}
}
if( dispType == 1 && dispMenu == 1 && iRow <= firstobs ) {
sprintf( Label, "" );
_settextcolor( MENUCOLOR );
_outxcr( Label, 9, relRow+1 );
}
if( dispType == 1 && dispMenu == 1 && iRow >= lastobs ) {
sprintf( Label, "" );
_settextcolor( MENUCOLOR );
_outxcr( Label, 9, relRow+1 );
}
if( dispType == 2 && dispMenu == 1 && iRow <= firstcum ) {
sprintf( Label, "" );
_settextcolor( MENUCOLOR );
_outxcr( Label, 9, relRow+1 );
}
if( dispType == 2 && dispMenu == 1 && iRow >= lastcum ) {
sprintf( Label, "" );
_settextcolor( MENUCOLOR );
_outxcr( Label, 9, relRow+1 );
}
/* debugging: print bar lengths
* sprintf( Label, "%2d", abar[iRow].length );
* _settextcolor( SINGLCOLOR );
* _outxcr( Label, 12, iRow );
*/
}
/*
* Standard Normal (Mean=0, Variance=1) Cumulative Distribution Function:
*
* PHI( x ) = 1.0 - 0.5 * erfcc( x divided by square root of 2 )
*/
void ClrScreen() {
int iRow, cRow;
cRow = _settextrows( tRows );
/* cRow = number of screen rows actually available <= tRows */
if( cRow == 43 ) {
topRow = 3;
botRow = 45;
}
else {
/* cRow = 25; */
topRow = 12;
botRow = 36;
}
_clearscreen( _GCLEARSCREEN );
_displaycursor( _GCURSOROFF );
rroot2 = 1.0 / sqrt( 2.0 );
/* cutval = lower limit for a row and upper limit for next row down */
cutval[0] = zvalue = 6.0;
expfrac[0] = exptail[0] = pgreater = 0.5 * erfcc( zvalue * rroot2 );
for( iRow = 1; iRow < 49; iRow++ ) {
zvalue -= 0.25;
cutval[iRow] = zvalue;
pequal = 0.5 * erfcc( zvalue * rroot2 );
if( zvalue >= 0.0 )
exptail[iRow] = pequal;
exptail[iRow+1] = 1.0 - pequal;
expfrac[iRow] = pequal - pgreater;
pgreater = pequal;
}
expfrac[49] = exptail[49];
cutval[49] = -99.99;
maxfrac = 0.0;
for( iRow = topRow; iRow <= botRow; iRow++ )
if( expfrac[iRow] > maxfrac )
maxfrac = expfrac[iRow];
}
void ResetHist() {
int iRow;
if( iduminit == 0 )
idum = initseed();
else if( iduminit > 0 )
idum = -iduminit ;
else
idum = iduminit ;
if( savfile != NULL )
fprintf( savfile, "Random Number Seed = %d\n", idum );
reps = (long)0;
exptime( ltime );
for( iRow = 0; iRow < 50; iRow++ )
totals[iRow] = (long)0;
}
void ObsrvHist() {
int iRow, flagobs, flagcum;
double obstail, cumtail;
for( iRow = 0; iRow < 50; iRow++ )
counts[iRow] = (long)0;
for( iter = 0; iter < reppbat; iter++ ) {
if( bmtype==0 )
zvalue = gasdev(&idum);
else
zvalue = box_mull(&idum);
for( iRow = 0; iRow < 24; iRow++ ) {
if( zvalue < cutval[24-iRow-1] && zvalue >= cutval[24-iRow] ) {
counts[24-iRow]++;
break;
}
if( zvalue < cutval[24+iRow] && zvalue >= cutval[24+iRow+1] ) {
counts[24+iRow+1]++;
break;
}
}
if( zvalue >= 6.0 )
counts[0]++;
if( zvalue < -6.0 )
counts[49]++;
}
reps += (long)reppbat;
chisqtot = chisqinc = obstail = cumtail = 0.0;
dofinc = doftot = flagobs = flagcum = 0;
firstobs = firstcum = -1;
lastobs = lastcum = 99;
for( iRow = 0; iRow < 50; iRow++ ) {
chisqobs[iRow] = chisqcum[iRow] = 0.0;
obsfrac[iRow] = (double)counts[iRow] / (double)reppbat;
totals[iRow] += counts[iRow];
cumfrac[iRow] = (double)totals[iRow] / (double)reps;
if( flagobs == 1 && exptail[iRow+1] * (double)reppbat < exptmin ) {
flagobs = 0;
lastobs = iRow;
}
if( flagobs == 0 )
obstail += obsfrac[iRow];
else {
chisqobs[iRow] = square( obsfrac[iRow] - expfrac[iRow] ) / expfrac[iRow];
dofinc++;
}
if( flagobs == 0 && lastobs == 99 &&
exptail[iRow] * (double)reppbat >= exptmin ) {
chisqobs[iRow] = square( obstail - exptail[iRow] ) / exptail[iRow];
flagobs = 1;
firstobs = iRow;
obstail = 0.0;
}
chisqobs[iRow] *= (double)reppbat;
chisqinc += chisqobs[iRow];
if( flagcum == 1 && exptail[iRow+1] * (double)reps < exptmin ) {
flagcum = 0;
lastcum = iRow;
}
if( flagcum == 0 )
cumtail += cumfrac[iRow];
else {
chisqcum[iRow] = square( cumfrac[iRow] - expfrac[iRow] ) / expfrac[iRow];
doftot++;
}
if( flagcum == 0 && lastcum == 99 &&
exptail[iRow] * (double)reps >= exptmin ) {
chisqcum[iRow] = square( cumtail - exptail[iRow] ) / exptail[iRow];
flagcum = 1;
firstcum = iRow;
cumtail = 0.0;
}
chisqcum[iRow] *= (double)reps;
chisqtot += chisqcum[iRow];
}
if( lastobs > 0 ) {
chisqobs[lastobs] = square( obstail - exptail[lastobs] ) / exptail[lastobs];
chisqobs[lastobs] *= (double)reppbat;
chisqinc += chisqobs[iRow];
dofinc++;
}
if( lastcum > 0 ) {
chisqcum[lastcum] = square( cumtail - exptail[lastcum] ) / exptail[lastcum];
chisqcum[lastcum] *= (double)reps;
chisqtot += chisqcum[lastobs];
doftot++;
}
}
/*
* initseed()...initialize pseudo-random sequence using the Microsoft
* long time() function to read system clock.
*/
int initseed() {
time_t stime;
int seed;
time( &stime );
seed = (int)stime ;
if( seed > 0 ) return( -seed );
else if( seed == 0 ) return( -13 );
else return( seed );
}
/*
* exptime()...time expired running SSS (using the Microsoft
* long time() function to read system clock.)
*/
time_t exptime( ltime )
time_t *ltime; {
time_t etime;
static time_t begin = (long)0;
time( &etime );
*ltime = etime ;
if( begin == (long)0 ) {
begin = etime ;
return (long)0;
}
else {
etime -= begin;
begin = *ltime;
return etime;
}
}
/*
* ranX()...return rntype value from ran1(), ran2(), or ran3().
*/
float ranX( idum )
int *idum; {
extern rntype;
if( rntype == 1 )
return ran1( idum );
else if( rntype == 2 )
return ran2( idum );
else if( rntype == 3 )
return ran3( idum );
else
return whran( idum );
}
/*
* Random number generator based upon: Wichman and Hill(1982),
* Algorithm AS 183,
* Applied Statistics 31, 188-190.
*/
float whran( idum )
int *idum; {
static int n1, n2, n3;
static double nn1, nn2, nn3;
static int ldum, ix=1, iy=1, iz=1; /* SEEDS */
static int iff=0;
double ixx, iyy, izz, rand;
if( *idum < 0 || iff == 0 ) {
ldum = (int)abs(*idum);
*idum = 1;
while( ldum > 30000 )
ldum = ldum%(30000);
if( ldum < 1 )
ldum = 1;
iff=1;
nn1 = n1 = 30269;
nn2 = n2 = 30307;
nn3 = n3 = 30323;
ix = iy = iz = ldum;
}
do {
ix = 171 * (ix%177) - 2 * (ix/177);
if( ix < 0 )
ix += 30269;
ixx = ix;
iy = 172 * (iy%176) - 35 * (iy/176);
if( iy < 0 )
iy += 30307;
iyy = iy;
iz = 170 * (iz%178) - 63 * (iz/178);
if( iz < 0 )
iz += 30323;
izz = iz;
rand = fmod( ixx/nn1 + iyy/nn2 + izz/nn3, 1.0 );
} while( rand==0.0 || rand==1.0 );
return (float)rand;
}
void SSSparm() {
printf( "\n\n\t*** Six Sigma Simulator ***\n");
printf( "\n\n\t SSS.EXE....Version 9207" );
printf( "\n\n\t A Quality Assurance Training Tool:" );
printf( "\n\t Statistics Committee of the QA Section of the PMA" );
printf( "\n\n\t Bob Obenchain, CompuServe User [72007,467]\n\n" );
strcpy( parnam, "sss.par" );
printf( "\n\n\tAt colon Prompts : ...simply press ENTER to get the [default]." );
printf( "\n\tThe filename for saving Keyboard input is to be : %s\n",
parnam );
printf( "\n\n\tRespond to the following prompts for Parameters...\n" );
printf( "\n\n\tType of Box-Muller Transformation ?" );
printf( "\n\tFast, Rejection Algorithm (type=0) or" );
printf( "\n\tSlow, Trignometric, Accurate (type=1) [%d] : ",
bmtype );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%d", &bmtype );
if( bmtype > 0 )
bmtype = 1 ;
else
bmtype = 0 ;
printf( "\n\n\tType of Portable, Uniform Pseudo-Random Generator ?" );
printf( "\n\t3 Linear Gongruential Gens. (type=1)" );
printf( "\n\tFast, Single Generator COMB (type=2)" );
printf( "\n\tSubtractive, Floating Knuth (type=3) or" );
printf( "\n\tWichman and Hill Algorithm (type=4) [%d] : ", rntype );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%d", &rntype );
if( rntype > 3 )
rntype = 4 ;
else if( rntype < 2 )
rntype = 1 ;
printf( "\n\n\tStart-up Seed ( 0 => use Clock ) [%d] : ",
iduminit );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%d", &iduminit );
if( iduminit > 0 )
iduminit = -iduminit ;
printf( "\n\n\tMinimum Expectancy (1 to 5) in Tail Cells [%4.1lf] : ", exptmin );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%lf", &exptmin );
if( exptmin < 1.0 || exptmin > 5.0 ) {
printf( "\n\tMinimum Expectancy will be 3.0, the default.\n");
exptmin = 3.0;
}
printf( "\n\n\tMaximum Total Number of Replications [%ld] : ", maxreps );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%ld", &maxreps );
if( maxreps > 2000000000 ) {
printf( "\n\tMaximum MAXREPS = 2,000,000,000.\n");
maxreps = 2000000000;
}
else if( maxreps < 10000 ) {
printf( "\n\tMinimum MAXREPS = 10,000.\n");
maxreps = 10000;
}
if( ( parfile = fopen( parnam, "w" ) ) == NULL ) {
printf( "\tCannot write to Keyboard save file : %s\n",
parnam );
return;
}
fprintf( parfile, "%6d ;number of replications per batch\n",
reppbat );
fprintf( parfile, "%6d ;Box-Muller type [0,1]\n", bmtype );
fprintf( parfile, "%6d ;random number type [1,2,3,4]\n", rntype );
fprintf( parfile, "%6d ;random number seed\n", iduminit );
fprintf( parfile, "%6d ;number of screen rows\n", tRows );
fprintf( parfile, "%6.1lf ;minimum expectancy in tail cells\n", exptmin );
fprintf( parfile, "%6ld ;maximum total replications\n", maxreps );
fclose( parfile );
}
void SampSize() {
printf( "\n\n\t*** Six Sigma Simulator ***\n");
printf( "\n\n\t SSS.EXE....Version 9207" );
printf( "\n\n\t A Quality Assurance Training Tool:" );
printf( "\n\t Statistics Committee of the QA Section of the PMA" );
printf( "\n\n\t Bob Obenchain, CompuServe User [72007,467]\n\n" );
printf( "\n\n\tSample Size now set at... %d Replications per Batch.",
reppbat );
printf( "\n\n\tSpecify Sample Size (10 to 1000) [%d] : ", reppbat );
gets( buf );
if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
sscanf( buf, "%d", &reppbat );
if( reppbat < 10 || reppbat > 1000 ) {
printf( "\n\tNumber of Replications per Batch will be 100, the default.\n");
reppbat = 100;
}
}
void main() {
int iRow;
if( !_setvideomode( _TEXTC80 ) )
if( !_setvideomode( _TEXTMONO ) ) {
_setvideomode( _DEFAULTMODE );
printf( "\n\nYou cannot run SSS. Your screen has only 40 Columns!" );
exit( 0 );
}
/* If monochrome or color burst disabled, use one color */
_getvideoconfig( &vc );
if( (vc.monitor == _MONO) || (vc.mode == _TEXTBW80) )
iColorMax = 1;
else
iColorMax = 15;
/* interrupt Ctrl-Break handler...return to text mode */
if( signal( SIGINT, handler ) == SIG_ERR ) {
printf("\nCouldn't set SIGINT...Abort!\n\n");
exit( 1 );
}
strcpy( parnam, "sss.par" );
if( ( parfile = fopen( parnam, "r" ) ) == NULL ) {
reppbat = 100;
bmtype = 1;
rntype = 1;
iduminit = 0;
tRows = 43; /* use 43 line mode if possible, else use 25 */
exptmin = 3.0;
maxreps = 2000000000; /* 2 billion */
}
else {
fgets( buf, 255, parfile );
sscanf( buf, "%d", &reppbat );
if( reppbat < 10 || reppbat > 1000 )
reppbat = 100;
fgets( buf, 255, parfile );
sscanf( buf, "%d", &bmtype );
if( bmtype > 0 )
bmtype = 1 ;
else
bmtype = 0 ;
fgets( buf, 255, parfile );
sscanf( buf, "%d", &rntype );
if( rntype > 3 )
rntype = 4 ;
else if( rntype < 2 )
rntype = 1 ;
fgets( buf, 255, parfile );
sscanf( buf, "%d", &iduminit );
fgets( buf, 255, parfile );
sscanf( buf, "%d", &tRows );
if( tRows != 25 )
tRows = 43;
fgets( buf, 255, parfile );
sscanf( buf, "%lf", &exptmin );
if( exptmin < 1.0 || exptmin > 5.0 )
exptmin = 3.0;
fgets( buf, 255, parfile );
sscanf( buf, "%ld", &maxreps );
if( maxreps > 2000000000 )
maxreps = 2000000000;
else if( maxreps < 10000 )
maxreps = 10000;
fclose( parfile );
}
if( iduminit == 0 )
idum = initseed();
else if( iduminit > 0 )
idum = -iduminit ;
else
idum = iduminit ;
strcpy( savnam, "sss.sav" );
if( ( savfile = fopen( savnam, "w" ) ) == NULL )
printf( "\tCannot write to Save filename : %s\n", savnam );
ClrScreen();
ResetHist();
dispType = 1; /* Incremental Histogram */
dispMenu = 0; /* Menu Off */
prevC = dispType;
memset( Hbar, BLOCK, 77 );
bwidth = 77;
Hbar[bwidth] = '\0';
fwidth = 77.5;
cellwid = 1;
while( TRUE ) {
ObsrvHist();
DrawHist( dispType );
if( RunMenu() == 1 || reps >= maxreps )
break;
}
if( savfile != NULL ) {
timer = exptime( ltime );
curtime = localtime( ltime );
fprintf( savfile, "Date/Time Stamp : %s", asctime( curtime ) );
fprintf( savfile, "Elapsed Simulation Time (sec) = %ld\n", timer );
fprintf( savfile, "Maximum Replications= %ld\n", maxreps );
fprintf( savfile, "\nSSS Parameter Settings...\n" );
fprintf( savfile, "%6d ;Box-Muller type [0,1]\n", bmtype );
fprintf( savfile, "%6d ;random number type [1,2,3,4]\n", rntype );
fprintf( savfile, "%6d ;replications per batch\n",
reppbat );
fprintf( savfile, "%6d ;number of screen rows\n", tRows );
fprintf( savfile, "\nSSS Summary Statistics...\n" );
fprintf( savfile, "Total Number of Replications = %ld\n", reps );
fprintf( savfile, "Minimum Expectancy in Tail Cells = %4.1lf\n", exptmin );
fprintf( savfile, "Cum. Chi Sq. = %8.2lf with d.f.= %2d\n", chisqtot, doftot );
fprintf( savfile, "Inc. Chi Sq. = %8.2lf with d.f.= %2d\n", chisqinc, dofinc );
fprintf( savfile, "\nRow CutVal ExpFract ExpTail CumFract ChiSq ObsFract ChiSq\n" );
for( iRow = 0; iRow < 50; iRow++ )
fprintf( savfile, "%2d %8.4lf %11.9lf %11.9lf %11.9lf %7.2lf %11.9lf %7.2lf\n",
iRow, cutval[iRow], expfrac[iRow], exptail[iRow],
cumfrac[iRow], chisqcum[iRow], obsfrac[iRow], chisqobs[iRow] );
}
_setvideomode( _DEFAULTMODE );
exit( 0 );
}